package com.qtcm.nas.cxm.xiaohaopin.web;

import com.qtcm.nas.cxm.xiaohaopin.pojo.Commodity;
import com.qtcm.nas.cxm.xiaohaopin.pojo.Crk;
import com.qtcm.nas.cxm.xiaohaopin.pojo.Deplete;
import com.qtcm.nas.cxm.xiaohaopin.service.IDepleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class DepleteController {
    @Autowired
    private IDepleteService depleteService;

    @RequestMapping("/findAllc")
    public String findAllComm(Model model) {
        List<Commodity> allComm = depleteService.findAllComm();
        model.addAttribute("all", allComm);
        return "add_Expenses";
    }

    @RequestMapping("addDep")
    public String addDep(Deplete deplete) {
        System.out.println(deplete.toString());
        depleteService.addDeplete(deplete);
        return "forward:findAllDep";
    }

    @RequestMapping("findAllDep")
    public String findAllDep(Model model) {
        List<Deplete> allDep = depleteService.findAllDep();
        model.addAttribute("allDep", allDep);
        return "Expenses";
    }

    @RequestMapping("findAD")
    public String findAD(Model model, int did) {
        List<Deplete> allDep = depleteService.findAllDep();
        Deplete depleteById = depleteService.findDepleteById(did);
        model.addAttribute("allDep", allDep);
        model.addAttribute("byId", depleteById);
        return "sign_In";
    }

    @RequestMapping("addCrk")
    public String addCrk(Deplete deplete, int zt, int rsl, int did) {
        Crk cr = new Crk();
        String j_letem_name = deplete.getJ_letem_name();
        cr.setH_Warehousing_time(new Date());
        cr.setJ_letem_name(j_letem_name);
        cr.setH_state(zt);
        depleteService.addCrk(cr);
        depleteService.updateRs(rsl, did);
        depleteService.updateDepR(cr.getH_Warehousing_time(), deplete.getJ_letem_name());
        return "expenses_Record";
    }

    @RequestMapping("findA")
    public String findA(Model model, int did) {
        List<Deplete> allDep = depleteService.findAllDep();
        Deplete depleteById = depleteService.findDepleteById(did);
        model.addAttribute("allDep", allDep);
        model.addAttribute("byId", depleteById);
        return "sign_Out";
    }

    @RequestMapping("addCrkc")
    public String addCrkc(Deplete deplete, int zt, int csl, int did) {
        Crk cr = new Crk();
        String j_letem_name = deplete.getJ_letem_name();
        cr.setH_Delivery_time(new Date());
        cr.setJ_letem_name(j_letem_name);
        cr.setH_state(zt);
        depleteService.addCrk(cr);
        depleteService.updateCk(csl, did);
        depleteService.updateDepC(cr.getH_Delivery_time(), deplete.getJ_letem_name());
        return "expenses_Record";
    }

    @RequestMapping("findZy")
    public String findZy(Model model, int history) {
        switch (history) {
            case 1:
                List<Crk> crk = depleteService.findCrk();
                Crk crk1 = crk.get(history);
                Long allJl = depleteService.findAllJl(crk1.getJ_letem_name());
                model.addAttribute("sj", allJl);
                model.addAttribute("crk", crk);
                break;
            case 2:
                List<Crk> rkSj = depleteService.findRkSj();
                Crk crk2 = rkSj.get(history);
                Long rk = depleteService.findRk(crk2.getJ_letem_name());
                model.addAttribute("sj", rk);
                model.addAttribute("crk", rkSj);
                break;
            case 3:
                List<Crk> ckSj = depleteService.findCkSj();
                Crk crk3 = ckSj.get(history);
                Long ck = depleteService.findCk(crk3.getJ_letem_name());
                model.addAttribute("sj", ck);
                model.addAttribute("crk", ckSj);
                break;
        }
        return "expenses_Record";
    }
}
